package com.hcf.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hcf.project.common.constant.RedisCacheConstant;
import com.hcf.project.common.convention.exception.ServiceException;
import com.hcf.project.common.convention.result.Result;
import com.hcf.project.common.convention.result.ResultTool;
import com.hcf.project.dao.entity.ShortLinkDO;
import com.hcf.project.dao.mapper.LinkMapper;
import com.hcf.project.dto.req.RecycleBinPageReqDTO;
import com.hcf.project.dto.req.RecycleBinRecoverReqDTO;
import com.hcf.project.dto.req.RecycleBinRemoveReqDTO;
import com.hcf.project.dto.req.RecycleBinSaveReqDto;
import com.hcf.project.dto.resp.ShortLinkPageRespDTO;
import com.hcf.project.service.RecycleBinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class RecycleBinServiceImpl extends ServiceImpl<LinkMapper, ShortLinkDO> implements RecycleBinService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 新增方法
     * @param requestParam
     * @return
     */
    @Override
    public Result<Void> saveRecycleBin(RecycleBinSaveReqDto requestParam) {
        LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 0);
        ShortLinkDO update = ShortLinkDO.builder().enableStatus(1).build();
        try {
            baseMapper.update(update, updateWrapper);
            // 删除缓存
            stringRedisTemplate.delete(RedisCacheConstant.GOTO_SHORT_LINK_KEY+requestParam.getFullShortUrl());
        }catch (Exception ex){
            throw new ServiceException("短链接加入回收站失败");
        }
        return ResultTool.success();
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(RecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> wrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 1)
                .orderByDesc(ShortLinkDO::getUpdateTime);
        IPage<ShortLinkDO> resultPage = baseMapper.selectPage(requestParam, wrapper);
        return resultPage.convert(item -> BeanUtil.toBean(item, ShortLinkPageRespDTO.class));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recoverShortLink(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 1);
        ShortLinkDO update = ShortLinkDO.builder().enableStatus(0).build();
        try {
            baseMapper.update(update, updateWrapper);
            // 删除空值缓存
            stringRedisTemplate.delete(RedisCacheConstant.GOTO_IS_NULL_SHORT_LINK_KEY+requestParam.getFullShortUrl());
        }catch (Exception ex){
            throw new ServiceException("回收站短链接恢复失败");
        }
    }


    /**
     * 删除回收站短链接
     * @param requestParam
     */
    @Override
    public void removeShortLink(RecycleBinRemoveReqDTO requestParam) {
        LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 1);
        try {
            baseMapper.delete(updateWrapper);
        }catch (Exception ex){
            throw new ServiceException("回收站短链接删除失败");
        }
    }
}
